package com.zegome.game.gomestory.main.scene.play.board;

import java.util.Random;
import java.util.Vector;

import com.badlogic.gdx.math.Vector2;

public class DiamondMod {
	
	Board board;
	int time, total;
	Vector<InfoTypePika> infoTypePika;
	int[][] mapData;
	
	public DiamondMod(Board board) {
		this.board = board;			
	}
	
	public void genMap(){				
		board.isPlaying = false;
		
		infoTypePika = new Vector<InfoTypePika>();
		infoTypePika.addAll(board.dataMap.infoTypePika);
		int leftPika = board.dataMap.mapCol*board.dataMap.mapRow - board.dataMap.total;    
		Vector<Integer> useAblePika = new Vector<Integer>();
		Vector<Possition> possitionAble = new Vector<Possition>();
		
	    for(int i=0;i<board.img.pika.length;i++){
	    	useAblePika.add(i);	    	
	    }
	    
	    for(int i=0; i< infoTypePika.size();i++)
	    	useAblePika.removeElementAt(infoTypePika.elementAt(i).type);
	    
	    int k;
	    
	    for(int i=0; i< leftPika/4; i++){
			k = new Random().nextInt(useAblePika.size());
			infoTypePika.add(new InfoTypePika(useAblePika.elementAt(k), 0));
			useAblePika.removeElementAt(k);
	    }	    
		
	    board.tickTime = 0f;
		board.dataMap.time = (int) ((board.dataMap.mapCol*board.dataMap.mapRow/2)*2.5f);
		board.dataMap.total = board.dataMap.mapCol*board.dataMap.mapRow;
		board.dataMap.totalPikaFix = board.dataMap.total;
		//
		if(mapData==null)
		mapData = new int[10][14];
		for(int row = 0; row < 10; row++){
			for( int col = 0; col < 14; col++){				
				mapData[row][col] = board.dataMap.map[row][col].type;				
			}
		}
		
		
		for(int col = 1; col <=board.dataMap.mapCol; col++){
		int i;
		boolean isFind;
		for(int row = 1; row<=board.dataMap.mapRow; row++)
		{			
			if(board.dataMap.map[row][col].type==-1){
				i = row+1;
				isFind = false;
				while (i<=board.dataMap.mapRow) {				
					if(board.dataMap.map[i][col].type>=0)
					{			
						isFind = true;
						break;
					}
					else{
						i++;
					}
				}				
				
				if(isFind == false)
				{						
				}
				else
				{					
					mapData[row][col] = mapData[i][col];
					mapData[i][col] = -1;
				}
			}
		}
		}
		//
		for(int row = 1; row<=board.dataMap.mapRow; row++){
			for(int col = 1; col <=board.dataMap.mapCol; col++){
				if(mapData[row][col]==-1){
					possitionAble.add(new Possition(row, col));
				}
			}
		}
		int rc;
		Random randPika = new Random();
		//
		while (possitionAble.size()!=0) {			
			k = randPika.nextInt(infoTypePika.size());
			rc = new Random().nextInt(possitionAble.size());
			
			mapData[possitionAble.elementAt(rc).row][possitionAble.elementAt(rc).col] = infoTypePika.elementAt(k).type;
			possitionAble.removeElementAt(rc);
			
			rc = new Random().nextInt(possitionAble.size());
			mapData[possitionAble.elementAt(rc).row][possitionAble.elementAt(rc).col] = infoTypePika.elementAt(k).type;
			possitionAble.removeElementAt(rc);
			
			infoTypePika.elementAt(k).total+=2;
		}	
		
		board.dataMap.infoTypePika.removeAllElements();
		board.dataMap.infoTypePika.addAll(infoTypePika);				
		board.dataMap.stylePlay = randPika.nextInt(5);
		
		board.isPlaying = true;
	}
	
	
	public void actionCol(int c){
	
		int typePika;
		
		Random rand = new Random();
		
		int start = board.dataMap.mapRow+1;
		
		float time = 0f;
		
		int i;
		
		boolean isFind;
		
		for(int row = 1; row<=board.dataMap.mapRow; row++)
		{			
			if(board.dataMap.map[row][c].type==-1){
				i = row+1;
				isFind = false;
				while (i<=board.dataMap.mapRow) {				
					if(board.dataMap.map[i][c].type>=0)
					{			
						isFind = true;
						break;
					}
					else{
						i++;
					}
				}				
				
				if(isFind == false)
				{	
					board.dataMap.map[row][c].set(mapData[row][c]);					
					board.dataMap.map[row][c].runFromTo2(time, 0.8f, new Vector2(
							board.dataMap.xBegin+c*board.dataMap.wi1Pikachu,
							board.dataMap.yBegin+start*board.dataMap.hi1Pikachu),
							new Vector2(board.dataMap.map[row][c].getX(), board.dataMap.map[row][c].getY()));
					time+=0.05f;
					start++;
				}
				else
				{					
					board.dataMap.map[row][c].set(board.dataMap.map[i][c].type);
					board.dataMap.map[i][c].set(-1);
					board.dataMap.map[row][c].runFromTo2(time, 0.8f, new Vector2(board.dataMap.map[i][c].getX(), board.dataMap.map[i][c].getY()),
							new Vector2(board.dataMap.map[row][c].getX(), board.dataMap.map[row][c].getY()));
					time+=0.05f;
				}
			}
		}
	}
}
